if (typeof (Edushare) == "undefined")
	Edushare = {};
if (typeof (Edushare.ActionPanel) == "undefined")
	Edushare.ActionPanel = {};

function getTimeMins(str) {
	var h = parseInt(str.substr(0,2),10);
	var m = parseInt(str.substr(3,2),10);
	var s = str.substr(6,2);
	if(s == "PM" && h!=12)
		h+=12;
	return (h*60+m);
};

Edushare.ActionPanel.Panels = function() {
	$('.es-actionBox').each(function(){
		var schPanel = "<div class='es-action' dest='schPanelAP'><a href='#'>Schedule Event</a></div>";
		//var assgnPanel = "<div class='es-action' dest='assgnPanelAP'><a href='#'>Create Assignment</a></div>";
		//for now no quick create assignment box. TODO: IMPLEMENT THIS!! 
		var assgnPanel = "";
		var filesPanel = "<div class='es-action' dest='filesPanelAP'><a href='#'>Upload Files</a></div>";
		var msgPanel = "<div class='es-action' dest='msgPanelAP'><a href='#'>Send a message</a></div>";
		
		var options = "";
		if($(this).attr('id') == "msgPanelAP"){
			if($('#schPanelAP').size()>0)
				options += schPanel;
			if($('#assgnPanelAP').size()>0)
				options += assgnPanel;
			if($('#filesPanelAP').size() > 0)
				options += filesPanel;
		}
		if($(this).attr('id') == "schPanelAP"){
			if($('#msgPanelAP').size()>0)
				options += msgPanel;
			if($('#assgnPanelAP').size()>0)
				options += assgnPanel;
			if($('#filesPanelAP').size() > 0)
				options += filesPanel;			
		}
		if($(this).attr('id') == "assgnPanelAP"){
			if($('#msgPanelAP').size()>0)
				options += msgPanel;
			if($('#schPanelAP').size()>0)
				options += schPanel;
			if($('#filesPanelAP').size() > 0)
				options += filesPanel;						
		}
		
		$(this).find('.js-swapper').html(options);
	});
	
	$('.es-actionBox').hide();
	$('.es-actionBox').first().show();
	$('.es-action > a').click(function(){
		var panel = $(this).parent().attr('dest');
		$('.es-actionBox').hide();
		$('#'+panel).show();
	});
	
};

$(document).ready(function() {
	Edushare.ActionPanel.Panels();
	
	$('#startDateSchPanel').datepicker().datepicker('option', {
		showAnim : 'fadeIn'
		}).datepicker('option', 'dateFormat', 'd M yy')
		.datepicker('setDate', new Date()).datepicker('setDate',new Date()).
		change(function() {
			var d1 = $.datepicker.parseDate('d M yy', $(this).val());
			var d2 = $.datepicker.parseDate('d M yy', $("#endDateSchPanel").val());
			if(d1 > d2)
				$("#endDateSchPanel").val($(this).val());
		}
	);
		
	$('#endDateSchPanel').datepicker().datepicker('option', {
		showAnim : 'fadeIn'
		}).datepicker('option', 'dateFormat', 'd M yy')
		.datepicker('setDate',new Date()).change(function() {
			var d1 = $.datepicker.parseDate('d M yy', $(this).val());
			var d2 = $.datepicker.parseDate('d M yy', $("#startDateSchPanel").val());
			if(d2 > d1)
				$("#startDateSchPanel").val($(this).val());
		}
	);

	var date = new Date();
	date.setMinutes(date.getMinutes()+10, 0, 0);
	if(date.getHours()==0){
		date = new Date();
	}
	
	var str = "";
	var hrs = date.getHours();
	
	if (hrs < 12)
	{
		str = " AM";
	}else
	{
		str = " PM";
	}
	if(hrs == 0)
	{
		hrs = 12;
	}
	if (hrs > 12)
	{
		hrs = hrs - 12;
	}

	var m = date.getMinutes();
	
	str = ((hrs>9)?(""+hrs):("0"+hrs))+":"+((m>9)?(""+m):("0"+m))+str;
		
	if($('#startTimeSchPanel').length > 0){
		$('#startTimeSchPanel').timeEntry({ampmPrefix: ' '}).val(str);
		$('#endTimeSchPanel').timeEntry({ampmPrefix: ' '}).val(str);
	}
	$('#ScheduleFormSchPanel').submit(function(){
		if($('#startDateSchPanel').val() == "" || $('#endDateSchPanel').val() =="" ||
				$('#endTimeSchPanel').val() == "" || $('#endDateSchPanel').val() == "" ||
				$('#endDateSchPanel').val()=="" || $("#nameSPCat").val() == ""){
			
			Edushare.FunctionalTextFields.SetError('Event title, start time, and end time are required fields.');
			return false;
		}
		var m1 = getTimeMins($("#startTimeSchPanel").val());
		var m2 = getTimeMins($("#endTimeSchPanel").val());
		if($("#startDateSchPanel").val() == $("#endDateSchPanel").val() && m1 > m2){			
			Edushare.FunctionalTextFields.SetError('Event start time must be less than end time.');
			return false;
		}
		
		$('#startTimeSPCat').val($('#startDateSchPanel').val() + ' ' +$('#startTimeSchPanel').val());
		$('#endTimeSPCat').val($('#endDateSchPanel').val() + ' ' +$('#endTimeSchPanel').val());
		return true;
	});
	
	$('#SPSubmit').click(function (evt){
		evt.stopPropagation();
	});
	
});